package com.luuyi.actionchain;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.luuyi.actionchain.action.AbsAction;
import com.luuyi.actionchain.action.Action;
import com.luuyi.actionchain.action.AsyncDelayAction;
import com.luuyi.actionchain.action.DialogAction;
import com.luuyi.actionchain.action.ToastAction;

public class CommonActionsActivity extends AppCompatActivity {

    private boolean isDoingAction;
    private DialogAction firstAction;
    private AsyncDelayAction secondAction;
    private ToastAction thirdAction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_actions);

        initActions();
        initViews();
    }

    private void initViews() {
        findViewById(R.id.tv_do_actions).setOnClickListener(v -> doActions());
        View layoutDialogAction = findViewById(R.id.layout_dialog);
        View layoutSleepAction = findViewById(R.id.layout_sleep);
        View layoutToastAction = findViewById(R.id.layout_toast);

        EditText etDialog = layoutDialogAction.findViewById(R.id.et_content);
        etDialog.setText(firstAction.getContent());
        etDialog.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                firstAction.setContent(s.toString());
            }
        });
        EditText etSleep = layoutSleepAction.findViewById(R.id.et_content);
        etSleep.setText(String.valueOf(secondAction.getDelayTime()));
        etSleep.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                try {
                    secondAction.setDelay(Long.valueOf(s.toString()));
                } catch (NumberFormatException e) {
                    secondAction.setDelay(AsyncDelayAction.DEFAULT_DELAY_TIME);
                }
            }
        });
        EditText etToast = layoutToastAction.findViewById(R.id.et_content);
        etToast.setText(thirdAction.getContent());
        etToast.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                thirdAction.setContent(s.toString());
            }
        });

        layoutDialogAction.findViewById(R.id.iv_close).setOnClickListener(v -> etDialog.setText(""));
        layoutSleepAction.findViewById(R.id.iv_close).setOnClickListener(v -> etSleep.setText(String.valueOf(0L)));
        layoutToastAction.findViewById(R.id.iv_close).setOnClickListener(v -> etDialog.setText(""));
    }

    /**
     * init actions
     */
    private void initActions() {
        firstAction = new DialogAction(this);
        secondAction = new AsyncDelayAction(this);
        thirdAction = new ToastAction(this);

        firstAction.setNext(secondAction);
        secondAction.setNext(thirdAction);
        thirdAction.setNext(new AbsAction() {
            @Override
            public void handle() {
                isDoingAction = false;
            }
        });
    }

    private void doActions() {
        if (isDoingAction) {
            Toast.makeText(this, "Actions is doing now.", Toast.LENGTH_SHORT).show();
            return;
        }
        firstAction.start();
    }
}